#include "PipeManager.h"
#include "../../include/ServerDefinition.h"
#include "GateServer.h"

CPipeManager::CPipeManager(void)
{
}


CPipeManager::~CPipeManager(void)
{
    TServerList::iterator i = m_servers.begin();
    for (; i != m_servers.end(); i++)
    {
        i->second->Release();
    }
}

void CPipeManager::on_connected(utility::network::pipe* pPipe)
{
    IServer * pServer = NULL;
    if (m_servers.find(pPipe->get_remote_id().number) == m_servers.end())
    {
        switch(pPipe->get_remote_id().detail_info.type)
        {
        case GATE_SERVER:
            pServer = new CGateServer(pPipe);
            break;
        default:
            return;
        }

        pPipe->set_pipe_event_handler(pServer);
        m_servers[pPipe->get_remote_id().number] = pServer;
    }
}

IServer * CPipeManager::Find( uint32 nId )
{
    TServerList::iterator i = m_servers.find(nId);
    if (i != m_servers.end())
        return i->second;
    return NULL;
}

IServer * CPipeManager::Find( utility::network::PIPEID oPipeId )
{
    return Find(oPipeId.number);
}

void CPipeManager::Remove( utility::network::PIPEID oPipeId )
{
    Remove(oPipeId.number);
}

void CPipeManager::Remove( uint32 nId )
{
    TServerList::iterator i = m_servers.find(nId);
    if (i != m_servers.end())
    {
        i->second->Release();
        m_servers.erase(i);
    }
}
